using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Security.Principal;

namespace RandomSearch.PluginsCommon {
    public static class SecurityHelper {
        public static Func<IdentityReference, bool> GetMemoizedIdentityChecker(WindowsIdentity currentIdentity) {
            Contract.Assert(null != currentIdentity);
            var cache = new Dictionary<string, bool>();
            return id =>
                       {
                           Contract.Assert(null != id);
                           string s = id.Value;
                           if (cache.ContainsKey(s)) return cache[s];

                           bool res = (id == currentIdentity.User) ||
                                      (null != currentIdentity.Groups && currentIdentity.Groups.Contains(id));

                           cache.Add(s, res);
                           return res;
                       };
        }
    }
}